//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    imageObject: {},
    userInfo: {},
    hasUserInfo: false,
    index: 0,
    uploadIcon: false,
    uploadText: '上传图片',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    items: [
      { name: 'USA', value: '同意' }
    ],
    addr:'地址',
    formCheck:'',
    tempFilePaths:[]
  },
  onLoad: function () {

   
  },
  checkboxChange: function (e) {
    console.log(e.detail.value)
  },
  formSubmit: function (e) {
    var that = this;
    var imgurl = that.data.imageObject.imageURL ? that.data.imageObject.imageURL : '';
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    if (e.detail.value.textarea == '' ) {
      that.setData({ formCheck:'发布内容不能为空' })     
    } else if(e.detail.value.tel == '') {
      that.setData({ formCheck: '手机不能为空' }) 
    } else if (e.detail.value.peizhi==''){
      that.setData({ formCheck: '选择协议' }) 
    }else{
      that.setData({ formCheck: '' })
      console.log(e.detail.value.peizhi)
      
      wx.request({
        // url: 'http://www.weshoppma.com/api/house/add_house',
        url: 'http://www.shop.com/api/info/add_info',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        data: {
          address: that.data.addr,
          desc: e.detail.value.textarea,
          tel: e.detail.value.tel,
          catid:33,
          uid:4
        },
        success: function (res) {
          if (res.data.status == 0) {
            wx.showToast({
              title: res.data.info,
              icon: 'loading',
              duration: 1500
            })

          } else {
            // wx.showToast({
            //   title: res.data.info,//这里打印出登录成功
            //   icon: 'success',
            //   duration: 2000
            // });
            // const uploadTask = wx.uploadFile({
            //   url: 'http://www.shop.com/api/info/add_image', //仅为示例，非真实的接口地址
            //   filePath: that.data.tempFilePaths[0],
            //   name: 'file',
            //   formData: {
            //     'infoId': res.data.id
            //   },
            //   success: function (res) {
            //     var data = res.data
            //     //do something
            //     wx.reLaunch({
            //       url: "../../pages/index/index"
            //     });
            //   }
            // })
            // uploadTask.onProgressUpdate((res) => {
            //   console.log('上传进度', res.progress)
            //   console.log('已经上传的数据长度', res.totalBytesSent)
            //   console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
            // })
            var pics = that.data.tempFilePaths;
            that.uploadimg({
              url: 'http://www.shop.com/api/info/add_image',//这里是你图片上传的接口
              path: pics,//这里是选取的图片的地址数组
              ifid: res.data.id
            })
          }
        }
      })
    }
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '自定义转发标题',
      success: function (res) {
        // 转发成功
        console.log(res.shareTickets);
        wx.getShareInfo({
          shareTicket: res.shareTickets,
          complete(res) {
            console.log(res)
          }
        })
        // console.log(res.shareTickets[0]);
      },
      fail: function (res) {
        // 转发失败
      }


    }
  },
  chooseLcat:function(){
    var that =this
    wx.chooseLocation({
      success(res){
        that.setData({ addr: res.address})
        console.log(res.address)
      }
    })
  },
  chooseImage:function(){
    var that =this,
      pics = that.data.tempFilePaths;
    if (pics.length<6){
      wx.chooseImage({
        count: 6 - pics.length, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var imgsrc = res.tempFilePaths
          pics = pics.concat(imgsrc);
          that.setData({ tempFilePaths: pics })

        }
      })
    }else{
      wx.showToast({
        title: '最多上传6张图片',
        icon: 'loading',
        duration: 1000
      });  
    }
    
  },
  previewImage: function (e) {
    var current = e.target.dataset.src

    wx.previewImage({
      current: current,
      urls: this.data.tempFilePaths
    })
  },
  uploadimg:function(data){
    var that = this,
      i = data.i ? data.i : 0,//当前上传的哪张图片
      success = data.success ? data.success : 0,//上传成功的个数
      fail = data.fail ? data.fail : 0;//上传失败的个数
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'file',//这里根据自己的实际情况改
        formData: {
          'infoId': data.ifid
        },
      success: (resp) => {
        success++;//图片上传成功，图片上传成功的变量+1
        console.log(resp)
        console.log(i);
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1
      },
      fail: (res) => {
        fail++;//图片上传失败，图片上传失败的变量+1
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);
        i++;//这个图片执行完上传后，开始上传下一张
        if (i == data.path.length) {   //当图片传完时，停止调用          
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);
        } else {//若图片还没有传完，则继续调用函数
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }

      }
    });
  }
})
